<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.qiqianyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\company\controller;

use app\admin\model\RouteModel;
use app\company\model\CompanyCategoryModel;
use cmf\controller\AdminBaseController;
use app\company\model\CompanyModel;
use think\Db;
use app\company\service\CategoryService;
use app\company\service\CompanyService;
use app\common\model\DistrictModel;


class AdminIndexController extends AdminBaseController
{

    public function index(){


        $companyService=   new CompanyService ();
        $param = $this->request->param();

       // $categoryId = $this->request->param('category', 0, 'intval');

        //$postService = new PostService();
        $companyList        = $companyService->adminArticleList($param);

        $companyList->appends($param);

        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('company_list', $companyList->items());
        $this->assign('page', $companyList->render());


        $companyCategoryList= CategoryService::queryCompanyCategoryList();
        $this->assign('company_category', $companyCategoryList);


        return $this->fetch();
    }


    /**
     * 打开添加页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){

        $companyCategoryList= CategoryService::queryCompanyCategoryList();
        $this->assign('company_category', $companyCategoryList);

        $companyNatureList = CategoryService::queryCompanyNatureList();
        $this->assign('company_nature', $companyNatureList);

        $memberLevelList = CategoryService::queryMemberLevelList();
        $this->assign('member_level', $memberLevelList);

        $memberQualityList = CategoryService::queryMemberQualityList();
        $this->assign('member_quality', $memberQualityList);


        $memberStatusList = CategoryService::queryMemberStatusList();
        $this->assign('member_status', $memberStatusList);


        $defaultDistrict = 0;
        $districts = $this->getDistrict($defaultDistrict);
        $this->assign("districts", $districts);
        $this->assign("default_district", $defaultDistrict);


        return $this->fetch();
    }



    public function addPost()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();



            $post   = $data['post'];

            //dump($data);

            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }

           //dump($data);

            $result = $this->validate($data['post'], 'Company');
            if ($result !== true) {
                $this->error($result);
            }

            $companyModel = new CompanyModel();
            $companyModel->adminAddCompany($data['post']);

            //$data['post']['id'] = $companyModel->id;


            $this->success('添加成功!');

        }

    }


    /**
     * 打开添加页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){

        $companyCategoryList= CategoryService::queryCompanyCategoryList();
        $this->assign('company_category', $companyCategoryList);

        $companyNatureList = CategoryService::queryCompanyNatureList();
        $this->assign('company_nature', $companyNatureList);

        $memberLevelList = CategoryService::queryMemberLevelList();
        $this->assign('member_level', $memberLevelList);

        $memberQualityList = CategoryService::queryMemberQualityList();
        $this->assign('member_quality', $memberQualityList);


        $memberStatusList = CategoryService::queryMemberStatusList();
        $this->assign('member_status', $memberStatusList);





        $id = $this->request->param('id', 0, 'intval');

        $companyModel = new CompanyModel();
        $post            = $companyModel->where('id', $id)->find();
        $this->assign('post', $post);

        $defaultDistrict = $post["district_id"];
        $districts = $this->getDistrict($defaultDistrict);
        $this->assign("districts", $districts);

        $this->assign("default_district", $defaultDistrict);



        return $this->fetch();
    }


    public function editPost()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();



            $post   = $data['post'];

            //dump($data);

            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }

            //dump($data);

            $result = $this->validate($data['post'], 'Company');
            if ($result !== true) {
                $this->error($result);
            }

            $companyModel = new CompanyModel();
            $companyModel->adminEditCompany($data['post']);

            //$data['post']['id'] = $companyModel->id;


            $this->success('修改成功!');

        }

    }

    public function getDistrict($id)
    {
        $districts = array(
            "name" => "",
            "options" => array(
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => "")
            )
        );
        $districtModel = new DistrictModel();
        $district = $districtModel->find($id);
        if (empty($district)) {
            $districts['options'][0]['options'] = $districtModel->where(array("parentid" => 0))->order("listorder")->select();;
        } else {
            if ($district['level'] < 4) {
                $districts['options'][$district['level']]["options"] = $districtModel->where(array("parentid" => $district['id']))->order("listorder")->select();
            }
            while ($district['level'] >= 1) {
                $districts['options'][$district['level'] - 1]["id"] = $district['id'];
                $districts['options'][$district['level'] - 1]["options"] = $districtModel->where(array("parentid" => $district['parentid']))->order("listorder")->select();
                $districts['name'] = $district['name'] . $districts['name'];
                $district = $districtModel->find($district['parentid']);
            }
        }
        return $districts;
    }


    public function district($parentid = 0, $type = "array")
    {
        $districtModel = new DistrictModel();
        $district = $districtModel->where(array("parentid" => $parentid))->order("listorder")->select();
        if ($type == "array") {
            return $district;
        } else {
            $this->result($district);
        }
    }


}
